home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
cool
/
ge_cool.lha
/
GE_COOL2.1
/
src
/
Stack
/
Base_Stack.C
next >
Wrap
C/C++ Source or Header
|
1992-04-22
|
8KB
|
218 lines
//
// Copyright (C) 1991 Texas Instruments Incorporated.
//
// Permission is granted to any individual or institution to use, copy, modify,
// and distribute this software, provided that this complete copyright and
// permission notice is maintained, intact, in all copies and supporting
// documentation.
//
// Texas Instruments Incorporated provides this software "as is" without
// express or implied warranty.
//
// Created: MBN 08/29/89 -- Initial design and implementation
// Updated: MJF 03/12/90 -- Added group names to RAISE
//
// The CoolStack class is publicly derived from the Generic class and is used to
// implement non-type specific functionality for the parameterized CoolStack class.
// In this manner, code common to all instances of the CoolStack class can be
// shared to reduce code replication. The CoolStack class implements a one
// dimensional vector of a user-specified type. This is accomplished by using
// the parameterized type capability of C++. The stack will grow dynamically
// as necessary with the amount of growth determined by the value of an
// allocation size slot. Fixed length stacks are also supported by setting the
// value of the allocation size slot to zero.
//
#ifndef BASE_STACKH // If no definition for CoolStack
#include <cool/Base_Stack.h> // Include definition file
#endif
// long set_length(long, char*) -- Change number of elements in this stack
// Input: Integer number of elements and
// character string of type
// Output: Number of elements
long CoolStack::set_length (long n, const char* Type) {
#if ERROR_CHECKING
if (n < 0) { // If index out of range
//RAISE (Error, SYM(CoolStack), SYM(Negative_Length),
printf ("CoolStack<%s>::set_length(): Negative length %d.\n", Type, n);
abort ();
}
#endif
if (n <= size) // If not greater than size
this->number_elements = n; // Set new length
else
this->number_elements = size; // Else set to maximum possible
return this->number_elements; // Return value
}
// void set_growth_ratio(float, char*) -- Set growth percentage of this stack
// Input: Float ratio and character string type
// Output: None
void CoolStack::set_growth_ratio (float ratio, const char* Type) {
#if ERROR_CHECKING
if (ratio <= 0.0) { // If non-positive growth
//RAISE (Error, SYM(CoolStack), SYM(Negative_Ratio),
printf ("CoolStack<%s>::set_growth_ratio(): Negative growth ratio %f.\n",
Type, ratio);
abort ();
}
#endif
this->growth_ratio = ratio; // Adjust instance ration
}
// void set_alloc_size(int, char*) -- Set the default allocation size
// Input: Integer size and character string type
// Output: None
void CoolStack::set_alloc_size (int size, const char* Type) {
#if ERROR_CHECKING
if (size < 0) { // If index out of range
//RAISE (Error, SYM(CoolStack), SYM(Negative_Size),
printf ("CoolStack<%s>::set_alloc_size(): Negative growth size %d.\n",
Type, size);
abort ();
}
#endif
this->alloc_size_s = size; // Set growth size
}
// CoolStack () -- Empty constructor for the CoolStack class
// Input: None
// Output: None
CoolStack::CoolStack () {
this->size = 0; // Initialize size
this->number_elements = 0; // Initialize element count
if (alloc_size_s == 0) // If no size specified
alloc_size_s = STACK_MEM_BLK_SZ; // Set the default size
this->growth_ratio = 0.0; // Intialize growth ratio
}
// CoolStack (long) -- constructor that specifies number of elements
// Input: Integer number of elements
// Output: None
CoolStack::CoolStack (long n) {
this->size = n; // Element capacity
this->number_elements = 0; // No elements
if (alloc_size_s == 0) // If no allocation size
alloc_size_s = STACK_MEM_BLK_SZ; // Set default size
this->growth_ratio = 0.0; // Initialize growth ratio
}
// CoolStack (CoolStack&) -- constructor for reference to another stack
// Input: CoolStack reference
// Output: None
CoolStack::CoolStack (const CoolStack& s) {
if (alloc_size_s == 0) // If no allocation size
alloc_size_s = STACK_MEM_BLK_SZ; // Default
this->growth_ratio = s.growth_ratio; // New growth ratio
this->size = s.size; // New size
this->number_elements = s.number_elements; // New number of elements
}
// ~CoolStack -- Destructor for CoolStack class that frees up storage
// Input: None
// Output: None
CoolStack::~CoolStack () {
}
// CoolStack& operator= (CoolStack&) -- Assigns this stack to another stack
// Input: Reference to a stack
// Output: Reference to modified this
CoolStack& CoolStack::operator= (const CoolStack& s) {
this->number_elements = s.number_elements; // New number of elements
this->growth_ratio = s.growth_ratio; // New growth ratio
return *this; // Return reference
}
// assign_error -- Error message for parameterized CoolStack<Type>::operator=()
// Input: Character string of type
// Output: None
void CoolStack::assign_error (const char* Type) {
//RAISE (Error, SYM(CoolStack), SYM(Static_Size),
printf ("CoolStack<%s>::operator=(): Static-size stack.\n", Type);
abort ();
}
// top_error -- Error message for parameterized CoolStack<Type>::top()
// Input: Character string of type
// Output: None
void CoolStack::top_error (const char* Type) {
//RAISE (Error, SYM(CoolStack), SYM(No_Elements),
printf ("CoolStack<%s>::top(): No elements in stack.\n", Type);
abort ();
}
// pop_error -- Error message for parameterized CoolStack<Type>::pop()
// Input: Character string of type
// Output: None
void CoolStack::pop_error (const char* Type) {
//RAISE (Error, SYM(CoolStack), SYM(No_Elements),
printf ("CoolStack<%s>::pop(): No elements in stack.\n", Type);
abort ();
}
// bracket_error -- Error message for parameterized CoolStack<Type>::operator[]()
// Input: Character string of type and index
// Output: None
void CoolStack::bracket_error (const char* Type, long n) {
//RAISE (Error, SYM(CoolStack), SYM(Out_Of_Range),
printf ("CoolStack<%s>::operator[](): Index %d out of range.\n", Type, n);
abort ();
}
// push_error -- Error message for parameterized CoolStack<Type>::push()
// Input: Character string of type
// Output: None
void CoolStack::push_error (const char* Type) {
//RAISE (Error, SYM(CoolStack), SYM(Static_Size),
printf ("CoolStack<%s>::push(): Static-size stack.\n", Type);
abort ();
}
// popn_error -- Error message for parameterized CoolStack<Type>::popn()
// Input: Character string of type and index
// Output: None
void CoolStack::popn_error (const char* Type, long n) {
//RAISE (Error, SYM(CoolStack), SYM(Static_Size),
printf ("CoolStack<%s>::popn(): Negative stack index %d.\n", Type, n);
abort ();
}
// resize_error -- Error message for parameterized CoolStack<Type>::resize()
// Input: Character string of type and size
// Output: None
void CoolStack::resize_error (const char* Type, long new_size) {
//RAISE (Error, SYM(CoolStack), SYM(Negative_Size),
printf ("CoolStack<%s>::resize(): Negative resize %d.\n", Type, new_size);
abort ();
}